<template>
    <div>
        <el-form ref="form" :model="obj.data" label-width="120px">
            <el-form-item label="选择物业员工" prop="user_id" :rules="[formRulesMixin.notNull]">
                <el-select v-model="obj.data.user_id" placeholder="选择物业员工" class="search_select" clearable>
                    <el-option v-for="(item,index) in obj.property_all" :key="index" :label="item.name" :value="item.property_id" :disabled="item.disabled" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <span class="btn clear" @click="$emit('close')">取消</span>
                <span class="btn sub" @click.prevent="upData">添加</span>
            </el-form-item>
        </el-form>
    </div>
</template>

<script setup>
    import { reactive,ref,getCurrentInstance,toRaw,inject } from 'vue'
    import DateRange from '@/components/date_range.vue'
    let { proxy } = getCurrentInstance()
    let obj = reactive({
        data:{
            drill_id: inject("id").value || 0,
            user_id: "",
        },
        property_all:[],
    })

    let setDate = d => { return d <= proxy.$utils.dateAddDay(new Date(), -1) }
    let init = () => {
        setusers()
    }

    let setusers = async ()=>{
        obj.property_all = await proxy.$utils.getProperty(proxy, '')
        proxy.$ajax({
            url: '/drill/user_list',
            data: {drill_id:obj.data.drill_id},
            bfLoading: true
        }).then(d => {
            if(d.success){
                let ary = d.msg.list
                ary.map(e=>{
                    obj.property_all.map(ev=>{
                        if(ev.property_id == e.property_id){
                            ev.disabled = true
                        }
                    })
                })
            }
        }).catch(() => { })
    }
    init()

    let upData = () => {
        proxy.$refs['form'].validate(valid => {
            if (valid) {
                let param = {
                    ...obj.data,
                }
                console.log(param);
                // return
                proxy.$ajax({
                    url: 'drill/user_add',
                    data: param,
                    bfLoading: true,
                }).then(d => {
                    if(d.success){
                        proxy.elMessageMixin(`添加成功`, 'success')
                        proxy.$emit('init')
                        proxy.$emit('close')
                    }else{
                        proxy.elMessageMixin(d.err, 'error')
                    }
                }).catch(() => { })
            }else{
                return false
            }
        })
    }
</script>

<style scoped>
    .checkboxgroup { padding-top: 5px; }
    .checkbox1 { height:22px; font-weight: normal; }
    .item { line-height: 0; }
    .subdiv { display: flex; flex-wrap: wrap; padding-left: 20px; }
    .subitem { padding-left:10px; }
    .subsubitem { padding-left: 30px; }
</style>